/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufjf.pos.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author cleber
 */
public abstract class GenericDao<G,K> {
    
    public abstract void inserir(G obj) throws SQLException, ClassNotFoundException;
    public abstract void alterar(G obj) throws SQLException, ClassNotFoundException;
    public abstract void apagar(G obj) throws SQLException, ClassNotFoundException;
    public abstract G pesquisaChave(K obj) throws SQLException, ClassNotFoundException;
    public abstract ArrayList<G> buscarTodos() throws SQLException, ClassNotFoundException;
    
    
    //CONSULTA
    public ResultSet executasql(String sql, List<String> param) throws SQLException, ClassNotFoundException{
        Connection c = ConnectionFactory.getConnection();
        PreparedStatement pst = c.prepareStatement(sql);
        
        int i = 1;
        for (String prm: param){
            pst.setString(i++, prm);
        }
        return pst.executeQuery();        
    }
    
    //DML - para update, delete, insert
    public int executaDML(String sql, List<String> param) throws SQLException, ClassNotFoundException{
        Connection c = ConnectionFactory.getConnection();
        PreparedStatement pst = c.prepareStatement(sql);
        
        int i = 1;
        for (String prm: param){
            pst.setString(i++, prm);
        }        
        return pst.executeUpdate();        
    }    
}
